package priv.zgh.forum.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import priv.zgh.forum.auth.entity.Power;
import priv.zgh.forum.auth.service.PowerService;
import priv.zgh.forum.utils.R;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZGH
 * @since 2021-10-22
 */
@CrossOrigin
@RestController
@RequestMapping("/auth/power")
public class PowerController {
    PowerService powerService;
    @Autowired
    public void setPowerService(PowerService powerService) {
        this.powerService = powerService;
    }

    @ApiOperation(value = "权限分页查询.")
    @GetMapping(path = "pagePower/{current}/{limit}")
    public R pagePower(@PathVariable long current, @PathVariable long limit){
        //填入当前页数与当前页所含项目数
        Page<Power> pageTeacher = new Page<>(current,limit);
        //调用教师service把数据封装进pageTeacher中
        powerService.page(pageTeacher,null);
        //取出数据
        long total = pageTeacher.getTotal();
        List<Power> teachers = pageTeacher.getRecords();
        //封装进返回json信息
        return R.ok().data("total",total).data("rows",teachers);
    }

    @ApiOperation(value = "按照传入条件来进行分页查询权限.")
    @PostMapping(path = "pagePowerCondition/{current}/{limit}")
    public R pagePowerCondition(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) Power query){
        Page<Power> powerPage = new Page<>(current,limit);
        QueryWrapper<Power> wrapper = new QueryWrapper<>();
        //取出查询封装的数据
        String path = query.getPath();
        //拼接动态SQL语句
        if(!StringUtils.isEmpty(path)){
            //构建条件
            wrapper.like("path",path);
        }
        //根据创建时间排序
        wrapper.orderByDesc("gmt_create");
        //将条件wrapper封装进service进行查询,然后将放回的结果封装进page
        powerService.page(powerPage,wrapper);
        //取出数据
        long total = powerPage.getTotal();
        List<Power> powers = powerPage.getRecords();
        //封装进返回json信息
        return R.ok().data("total",total).data("rows",powers);
    }

    @ApiOperation(value = "按照传入ID删除权限信息.")
    @DeleteMapping(path = "/delete/{id}")
    public R remoteTeacher(@ApiParam(name = "id",value = "讲师ID",required = true) @PathVariable(name = "id") String id){
        boolean flush = powerService.removeById(id);
        if(flush){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation(value = "根据权限ID来获取权限信息.")
    @GetMapping(path = "/getPower/{id}")
    public R getTeacherById(@PathVariable String id){
        Power power = powerService.getById(id);
        return R.ok().data("power",power);
    }

    @ApiOperation(value = "保存权限信息.")
    @PostMapping(path = "/addPower")
    public R addTeacher(@RequestBody Power power){
        boolean save = powerService.save(power);
        if(save){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @ApiOperation(value = "更新此ID的权限信息.")
    @PostMapping(path = "/updatePower")
    public R updateTeacher(@RequestBody Power power){
        boolean flush = powerService.updateById(power);
        if(flush){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @ApiOperation(value = "获取所有权限信息.")
    @GetMapping(path = "/getAll")
    public R updateTeacher(){
        return R.ok().data("powers",powerService.list(null));
    }
}

